package de.tabuma.pm.security;

import de.tabuma.lib.*;
import de.tabuma.lib.contentmanagement.entity.*;
import de.tabuma.pm.security.controller.*;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.users.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.6.1 $
 * @since $Date: 2010/01/13 10:19:20 $
 */
public class SecurityProviderInstanceController extends tabumacontroller {

    private static Logger log = Logger.getLogger(SecurityProviderInstanceController.class.getName());
    public SecurityProviderInstanceController(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }
    
    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        try {
            String ssoId = "SSO Project ID";
            String securityProviderUrl = "security provider url";
            String username = "username";
            String password = "password";
            String activate = "activate";

            String entityName = "security provider";
            String groupManagement = "group management";

            out.println("<div class=\"contentContent\">For standard security provider leave url, username, securityProvider Id and sso Project Id empty. The standard security provider cannot be changed except activity.</div>");

            if(request.getParameter(de.tabuma.pm.constants.SECURITY_PROVIDER_GROUP_MANAGEMENT)!=null) {
                GroupController groupController = new GroupController(this.getServlet(), this.getParameter());
                groupController.navigate(context, request, response, out);
                return;
            }

            Content content = new Content();

            Vector<Field> editableFields = new Vector<Field>();
            editableFields.add(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("securityProviderUrl"));
            editableFields.add(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("managementUsername"));
            editableFields.add(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("managementPassword"));
            editableFields.add(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("ssoProjectId"));
            editableFields.add(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("activ"));
            content.FormContent.setEditableFields(editableFields);

            Vector<Integer> editableTypes = new Vector<Integer>();
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_INTEGER));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_BOOLEAN));
            content.FormContent.setEditableFieldTypes(editableTypes);
            content.FormContent.setEntityClass(de.tabuma.pm.security.SecurityProviderInstance.class);
            content.FormContent.setEntityName(entityName);
            content.FormContent.setFacadeObject(new SecurityProviderInstanceFacade());

            UserService userService = UserServiceFactory.getUserService();
            com.google.appengine.api.users.User user = userService.getCurrentUser();
            String projectId = request.getParameter(de.tabuma.pm.constants.projectId);
            content.ListContent.setListParameterValue(""+projectId);

            Vector<Field> staticFields = new Vector<Field>();
            staticFields.add(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("projectId"));
            content.FormContent.StaticContent.setStaticFields(staticFields);

            Vector<Integer> staticFieldsType = new Vector<Integer>();
            staticFieldsType.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_INTEGER));
            content.FormContent.StaticContent.setStaticFieldsTyp(staticFieldsType);

            Vector<String> staticFieldsValue = new Vector<String>();
            staticFieldsValue.add(projectId);
            content.FormContent.StaticContent.setStaticFieldsValue(staticFieldsValue);
            
            content.GeneralContent.setParamName(de.tabuma.pm.constants.SECURITY_PROVIDER_PARAM);
            
            Vector<Field> listableFields = new Vector<Field>();
            listableFields.add(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("securityProviderUrl"));
            content.ListContent.setListableFields(listableFields);
            
            Hashtable<String,Hashtable<String,String>> additionalListFields = new Hashtable<String,Hashtable<String,String>>();

            Hashtable<String,String> parameterHash = (Hashtable<String,String>) this.getParameter().clone();
            parameterHash.put(de.tabuma.pm.constants.SECURITY_PROVIDER_GROUP_MANAGEMENT, "true");
            additionalListFields.put(groupManagement, parameterHash);

            content.ListContent.setAdditionalListFields(additionalListFields);
            
            content.GeneralContent.setServlet(this.getServlet());
            content.GeneralContent.setParameter(this.getParameter());
            
            Hashtable<Field, String> displayFields = new Hashtable<Field, String>();
            displayFields.put(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("securityProviderUrl"), securityProviderUrl);
            displayFields.put(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("managementUsername"), username);
            displayFields.put(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("managementPassword"), password);
            displayFields.put(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("ssoProjectId"), ssoId);
            displayFields.put(de.tabuma.pm.security.SecurityProviderInstance.class.getDeclaredField("activ"), activate);
            content.FormContent.setDisplayedName(displayFields);
            
            Hashtable<Field, List> listParameterContent = new Hashtable<Field, List>();
            content.ListContent.setListParameterContent(listParameterContent);
            
            Hashtable<Field,List<Field>> listParameterContentDisplayedFields = new Hashtable<Field, List<Field>>();
            content.ListContent.setListParameterDisplayedField(listParameterContentDisplayedFields);
            content.GeneralContent.setUser(user.getEmail());
            
            out.println(content.navigate(request));

        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }
}